package com.jojo.controller;

import com.jojo.entity.Result;
import com.jojo.entity.TimerInfo;
import com.jojo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Timer;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("oneKeySubmitOrderPage")
    public String oneKeySubmitOrderPage() {
        return "oneKeySubmitOrderPage";
    }

    @GetMapping("timingSubmitOrderPage")
    public String timingSubmitOrderPage() {
        return "timingSubmitOrderPage";
    }

    @PostMapping("oneKeySubmitOrder")
    @ResponseBody
    public Result oneKeySubmitOrder(@RequestBody HashMap<String, String> map) {
        try {
            return orderService.oneKeySubmitOrder(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(-1, "下单失败");
    }

    @PostMapping("timingSubmitOrder")
    @ResponseBody
    public Result timingSubmitOrder(@RequestBody HashMap<String, String> map) {
        try {
            return orderService.timingSubmitOrder(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(-1, "设定失败");

    }
    //获取已设定的定时任务
    @GetMapping("getTimeList")
    @ResponseBody
    public Result getTimeList() {
           List<TimerInfo> timerList = orderService.getTimeList();
        return Result.success("获取定时任务列表",timerList);
    }
    //取消定时任务
    @PostMapping("cancelTimer")
    @ResponseBody
    public Result cancelTimer(@RequestBody TimerInfo timerInfo) {
        try {
            return orderService.cancelTimer(timerInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(-1, "设定失败");

    }
}
